import { message } from "antd"
// 基础路由
const baseUrl='http://jacklv.cn'

function request(url,optinons={}){
    // 如果传入方法不为get
    if(optinons.method && optinons.method.toLowerCase()!=='get'){
        optinons.body=JSON.stringify(optinons.body || {})
        optinons.headers={
            "Content-Type":"application/json",
        }
    }
    // 修改headers，增加token
    optinons.headers={
        ...(optinons.headers||{}),
        token:localStorage.token
    }
    return fetch(baseUrl+url,optinons)
    .then((response)=>{
        // console.log('request的response',response);
        switch(response.status){
            case 200:
                return  response.json()
            case 401:
                // token过期
                localStorage.clear
                location.pathname='/Login'
                return Promise.reject()
        }
      
    }).then(res=>{
        console.log('res',res);
        if(res.code===1){
            return res.data
        }else{
            message.error(res.msg)
            return Promise.reject(res)
        }
    })
}

export default request